package com.yixin.vuehr.service;


import com.yixin.vuehr.util.HrUtils;
import com.yixin.vuehr.mapper.HrMapper;
import com.yixin.vuehr.mapper.HrRoleMapper;
import com.yixin.vuehr.model.Hr;
import com.yixin.vuehr.util.OpreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class HrService implements UserDetailsService {
    @Autowired
    HrMapper hrMapper;
    @Autowired
    HrRoleMapper hrRoleMapper;
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUserName(username);
        if (hr == null){
            throw new UsernameNotFoundException("用户名不存在！");
        }
        hr.setRoles(hrMapper.getHrRolesById(hr.getId()));
        OpreateUtil.addOp(hr.getId(),hr.getName()+"登入系统");
        return hr;
    }

    @Transactional
    public Integer createHr(Hr hr) {
        if (hrMapper.loadUserByUserName(hr.getUsername())!=null){
            throw new UsernameNotFoundException("用户已经存在！");
        }
        //Hr hr = hrMapper.loadUserByUserName(hr.getUsername());
        if (hr.getUserface()==null){
            hr.setUserface("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
        }
        String encodePassword = encoder.encode(hr.getPassword());
        hr.setPassword(encodePassword);
        hr.setEnabled(true);
        return hrMapper.insertSelective(hr);
    }

    public List<Hr> getAllHrs(String keywords) {
        return hrMapper.getAllHrs(HrUtils.getCurrentHr().getId(),keywords);
    }

    public Integer updateHr(Hr hr) {
        return hrMapper.updateByPrimaryKeySelective(hr);
    }

    @Transactional
    public boolean updateHrRole(Integer hrid, Integer[] rids) {
        hrRoleMapper.deleteByHrId(hrid);
        return hrRoleMapper.addRole(hrid,rids) ==  rids.length;
    }

    public Integer deleteHrById(Integer id) {
        return hrMapper.deleteByPrimaryKey(id);
    }

    public List<Hr> getAllHrsExceptCurrentHr() {
        return hrMapper.getAllHrsExceptCurrentHr(HrUtils.getCurrentHr().getId());
    }

    public Integer updateHrUserFace(String url) {
        Hr hr = new Hr();
        hr.setId(HrUtils.getCurrentHr().getId());
        hr.setUserface(url);
        return updateHr(hr);
    }

    public Integer updateHrPasword(String password) {
        Hr hr = new Hr();
        hr.setId(HrUtils.getCurrentHr().getId());
        hr.setPassword(encoder.encode(password));
        return updateHr(hr);
    }
}
